#include "StdAfx.h"
#include "TGSvrNetMan.h"
#include "stdio.h"


int TGSvrNetMan::vTcpCmdDispatch(SOCKET hClientSocket,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;

	SOCKADDR_IN addrPeer;
	int nAddrLen = sizeof(addrPeer);
	getpeername(hClientSocket, (PSOCKADDR) &addrPeer, &nAddrLen);

	char szPeerIP[128] = { 0 };
	sprintf(szPeerIP, "%s", inet_ntoa(addrPeer.sin_addr));

#ifdef dkq_test_svrnet
	//((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"in nMsgType:%d",pMsgHead->nMsgType);
#endif

	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_LOGIN:
		{
		}
		break;
	case MSG_TYPE_REALSTREAM:
		{
			nRet = TG_ERR_NETWORK_FAIL_CONNECT;	
		}
		break;
	case MSG_TYPE_TALK:
		{

		}
		break;
	case MSG_TYPE_BROADCAST:
		{
		}
		break;
	case MSG_TYPE_FILE:
		{
		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
		}
		break;
	}

#ifdef dkq_test_svrnet
	//((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"out nMsgType:%d",pMsgHead->nMsgType);
#endif

	if( TG_ERR_SUCCESS != nRet )
	{
		pRetMsgHead->nFrameSize = 0;
		pRetMsgHead->nErrFlag   = nRet;

		DWORD dwRet = 0;
		TGSendDataS(hClientSocket,
			(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),
			&dwRet,
			TGListenThread::m_hWSACmdSREvent,
			TGListenThread::m_hWSAExitEvent,TG_WAITTIME);

		//Sleep(50);

		TGShutDownSocket( hClientSocket );
		TGCloseSocket( hClientSocket );
		hClientSocket = NULL;
	}

	return nRet;
}

int TGSvrNetMan::vUdpCmdDispatch(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;

	char szPeerIP[128] = { 0 };
	sprintf(szPeerIP, "%s", inet_ntoa(pAddr->xAddr.sin_addr));

	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_REALSTREAM:
		{
			nRet = TG_ERR_NETWORK_FAIL_CONNECT;	
		}
		break;
	case MSG_TYPE_TALK:
		{
		}
		break;
	case MSG_TYPE_BROADCAST:
		{
		}
		break;
	case MSG_TYPE_FILE:
		{
		}
		break;
	default:
		{
			nRet = UdpCmdCommand(hSocket,pAddr,pMsgHead,pBuffer);
		}
		break;
	}

	if( TG_ERR_SUCCESS != nRet )
	{
		pRetMsgHead->nFrameSize = 0;
		pRetMsgHead->nErrFlag   = nRet;

		sendto( hSocket,
			(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),
			0,
			(struct sockaddr*)&pAddr->xAddr,pAddr->nAddrLen );
	}

	return nRet;
}

int TGSvrNetMan::UdpCmdCommand(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	switch( pMsgHead->nMsgType )
	{
	case 1:
		{

		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
			break;
		}
	}

	return nRet;
}